#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL)
    {
    }
};
class Solution
{
public:
    ListNode *FindFirstCommonNode(ListNode *pHead1, ListNode *pHead2)
    {
        int len1=0,len2=0;
        ListNode*cur1=pHead1,*cur2=pHead2;
        while(cur1) 
        {
            len1++;
            cur1=cur1->next;
        }
        while(cur2)
        {
            len2++;
            cur2=cur2->next;
        }
        cur1=pHead1,cur2=pHead2;
        int tem=abs(len1-len2);
        if(len1<len2)
        {
            swap(len1,len2);
            swap(cur1,cur2);
        }
        while(tem--)
        {
            cur1=cur1->next;
        }
        while(cur1&&cur2)
        {
            if(cur1==cur2)
            {
                return cur1;
            }
            cur1=cur1->next;
            cur2=cur2->next;
        }
        return nullptr;

    }
};
